package ryarithsGame;

import com.jme.scene.*;

import com.jme.math.Quaternion;
import com.jme.math.Vector3f;

import com.jme.bounding.*;


public class Bullets extends Node{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BulletNode[] shots, enemyShots;
	private BulletNode placeholder;
	private Node myRoot;
	public Bullets(String name, Node r)
	{
		super(name);
		myRoot = r;
		makeShots();
		
	}
	public int getShots()
	{
		return 1;
	}
	
	
	public int CheckCollisions(BoundingVolume bv)
	{
		//System.out.println(this.getChildren().size());
		for(int i = 0; i < shots.length; i++)
		{
			if(shots[i].isSet())
			{
				if (shots[i].CheckCollision(bv)== 1)
				{
					myRoot.detachChildNamed(shots[i].getName());
					return 1;
				}
			}
		}
		
		return 0;
	}
	
	
	public int CheckEnemyShotCollisions(BoundingVolume bv)
	{
		//System.out.println(this.getChildren().size());
		for(int i = 0; i < enemyShots.length; i++)
		{
			if(enemyShots[i].isSet())
			{
				if (enemyShots[i].CheckCollision(bv)== 1)
				{
					myRoot.detachChildNamed(enemyShots[i].getName());
					return 1;
				}
			}
		}
		
		return 0;
	}
	
	
	
	private void makeShots()
	{
		//SecureRandom random = new SecureRandom();
		
		//placeholder shot, behind camera
		placeholder = new BulletNode("placeholder", 0f);
		placeholder.setLocalTranslation(new Vector3f(0,0,50));
		myRoot.attachChild(placeholder);
		shots = new BulletNode[100];
		enemyShots = new BulletNode[100];
		
		for(int i = 0; i < shots.length; i++)
		{
			enemyShots[i] = new BulletNode("enemyShot" + i, 38f);
			shots[i] = new BulletNode("shot" + i, 300f);
			//shots[i].setLocalTranslation(new Vector3f(random.nextFloat() * 10, random.nextFloat() * 10, 0));	
		}
	}
	
	public void Fire(Vector3f gunPos, Vector3f gunDir, Quaternion gunRot)
	{
		
		for(int i = 0;i< shots.length; i++)
		{
		if(!shots[i].isSet())
		{
			shots[i].Fire(gunPos, gunDir, gunRot);
			myRoot.attachChild(shots[i]);
			myRoot.updateRenderState();
			return;
		}
		}
	}
	
		
	public void updateShots(float tpf)
	{
		for(int i = 0; i < shots.length; i++)
		{
			if(shots[i].isSet())
			{
				shots[i].updateShot(tpf);
				if(!shots[i].isSet())
				{
					myRoot.detachChildNamed(shots[i].getName());
				}
			}
		}
		for(int i = 0; i < enemyShots.length; i++)
		{
			if(enemyShots[i].isSet())
			{
				enemyShots[i].updateShot(tpf);
				if(!enemyShots[i].isSet())
				{
					myRoot.detachChildNamed(enemyShots[i].getName());
				}
			}
		}
	}
	
	public BulletNode[] getBullets()
	{
		return shots;
	}	

	public Vector3f[] getBulletsLoc()
	{/*
		int count = 0;
		for(int j =0; j < shots.length; j++)
		{
			if(shots[j].isSet())
			{
				count++;
			}
		}
		*/
		Vector3f[] loc = new Vector3f[shots.length];
		for(int i =0; i < shots.length; i++)
		{
			if(shots[i].isSet())
			{
				loc[i].set(shots[i].getLocalTranslation());
			}
			else
			{
				loc[i].set(new Vector3f(-1,-1,-1));
			}
		}
		
		
		return loc.clone();
	}

}